package willevaluate.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.github.mikephil.charting.charts.LineChart;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.MyLogUtils;
import utils.UnitUtils;
import view.MyLineChart;
import willevaluate.bean.LineChartBean;
import willevaluate.bean.WillCollegeDetailBean;

/**
 * Created by Administrator on 2017/5/6.
 * 志愿评估的高校详情
 */

@ContentView(R.layout.will_college_detail)
public class WillCollegeDetail extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView commom_title;
    @ViewInject(R.id.will_college_detail_chart)
    LineChart will_college_detail_chart;
    @ViewInject(R.id.will_college_detail_probability)
    private TextView will_college_detail_probability;
    @ViewInject(R.id.will_college_detail_low_count)
    private TextView will_college_detail_low_count;
    @ViewInject(R.id.will_college_detail_low_count_view)
    private View will_college_detail_low_count_view;
    @ViewInject(R.id.will_college_detail_low_count_detail)
    private TextView will_college_detail_low_count_detail;
    @ViewInject(R.id.will_college_detail_my_count)
    private TextView will_college_detail_my_count;
    @ViewInject(R.id.will_college_detail_my_count_view)
    private View will_college_detail_my_count_view;
    @ViewInject(R.id.will_college_detail_line)
    private TextView will_college_detail_line;
    @ViewInject(R.id.will_college_detail_spinner)
    private Spinner will_college_detail_spinner;
    @ViewInject(R.id.will_college_detail_ima)
    private ImageView will_college_detail_ima;

    private ArrayList<LineChartBean> arrayList;
    private boolean isSpinnerShow;

    private String college;
    private String type;//文理科
    private String count;
    private String rank;
    private String probability;//概率
    MyLineChart myLineChart;
    private int lineChartType;//折线图的类型：分数/排名

    List<WillCollegeDetailBean.CountlistBean> countlist;
    List<WillCollegeDetailBean.RanklistBean> ranklist;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    WillCollegeDetailBean willCollegeDetailBean = gson.fromJson((String) msg.obj, WillCollegeDetailBean.class);
                    if (willCollegeDetailBean != null) {

                        int lowest = Integer.parseInt(willCollegeDetailBean.getLowest());
                        int mycount = Integer.parseInt(willCollegeDetailBean.getMycount());
                        will_college_detail_low_count.setText(willCollegeDetailBean.getLowest());
                        will_college_detail_my_count.setText(willCollegeDetailBean.getMycount());
                        //按比例显示分数条
                        double lowest1 = lowest / 1.0;
                        double mycount1 = mycount / 1.0;
                        if (lowest1 >= mycount1) {
                            double rate = mycount1 / lowest1;
                            double v = 72 * rate;
                            ViewGroup.LayoutParams layoutParams = will_college_detail_low_count_view.getLayoutParams();
                            layoutParams.height = UnitUtils.dip2px(72);
                            will_college_detail_low_count_view.setLayoutParams(layoutParams);
                            ViewGroup.LayoutParams layoutParams1 = will_college_detail_my_count_view.getLayoutParams();
                            layoutParams1.height = UnitUtils.dip2px((int) v);
                            will_college_detail_my_count_view.setLayoutParams(layoutParams1);
                        } else {
                            double rate = lowest1 / mycount1;
                            double v = 72 * rate;
                            ViewGroup.LayoutParams layoutParams = will_college_detail_my_count_view.getLayoutParams();
                            layoutParams.height = UnitUtils.dip2px(72);
                            will_college_detail_my_count_view.setLayoutParams(layoutParams);
                            ViewGroup.LayoutParams layoutParams1 = will_college_detail_low_count_view.getLayoutParams();
                            layoutParams1.height = UnitUtils.dip2px((int) v);
                            will_college_detail_low_count_view.setLayoutParams(layoutParams1);
                        }
                        countlist = willCollegeDetailBean.getCountlist();
                        ranklist = willCollegeDetailBean.getRanklist();
                        changeLineChart();
                        will_college_detail_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                if (isSpinnerShow) {
                                    isSpinnerShow = false;
                                    will_college_detail_ima.setImageResource(R.mipmap.oa_expandlv_close_icon);
                                }
                                if (lineChartType != position) {
                                    lineChartType = position;
                                    changeLineChart();
                                }
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        college = getIntent().getStringExtra("college");
        type = getIntent().getStringExtra("type");
        count = getIntent().getStringExtra("count");
        rank = getIntent().getStringExtra("rank");
        probability = getIntent().getStringExtra("probability");

        lineChartType = MyLineChart.RANK;
        initView();
        initSpinner();
        getDataFromInterNet();
    }

    private void initView() {
        common_back.setOnClickListener(this);
        commom_title.setText(college);
        will_college_detail_probability.setText(probability);
        arrayList = new ArrayList<>();
        if("art".equals(type)){
            will_college_detail_low_count_detail.setText("16年文科最低分");
            will_college_detail_line.setText("往年录取分数线(广东文科)");
        }else{
            will_college_detail_low_count_detail.setText("16年理科最低分");
            will_college_detail_line.setText("往年录取分数线(广东理科)");
        }

    }

    private void initSpinner() {
        List<String> list = new ArrayList<>();
        list.add("省内排名");
        list.add("录取最低分");
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.will_college_detail_spinner_title, R.id.tv_spinner);
        adapter.add(list.get(0));
        adapter.add(list.get(1));
        adapter.setDropDownViewResource(R.layout.will_college_detail_spinner_item);
        will_college_detail_spinner.setAdapter(adapter);
        will_college_detail_spinner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!isSpinnerShow) {
                    isSpinnerShow = true;
                    will_college_detail_ima.setImageResource(R.mipmap.oa_expandlv_show_icon);
                }
                return false;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isSpinnerShow) {
            isSpinnerShow = false;
            will_college_detail_ima.setImageResource(R.mipmap.oa_expandlv_close_icon);
        }
        return super.onTouchEvent(event);
    }

    private void getDataFromInterNet() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("college", college);
        hashMap.put("type", type);
        hashMap.put("count", count);
        hashMap.put("rank", rank);
        new InterNetController(this, Constant.EVALUTECOLLEGEDETAIL, handler, hashMap, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            default:
                break;
        }
    }


    private void changeLineChart() {
        arrayList = new ArrayList<>();
        switch (lineChartType) {
            case MyLineChart.SCORE:
                if (countlist != null) {
                    if (countlist.size() > 0) {
                        for (int i = 0; i < countlist.size(); i++) {
                            LineChartBean lineChartBean = new LineChartBean();
                            lineChartBean.setNum(countlist.get(i).getCount());
                            lineChartBean.setYear(countlist.get(i).getYear());
                            arrayList.add(lineChartBean);
                        }
                        myLineChart = new MyLineChart(WillCollegeDetail.this, will_college_detail_chart, arrayList, lineChartType);
                    }
                }
                break;
            case MyLineChart.RANK:
                if (ranklist != null) {
                    if (ranklist.size() > 0) {
                        for (int i = 0; i < ranklist.size(); i++) {
                            LineChartBean lineChartBean = new LineChartBean();
                            lineChartBean.setNum(ranklist.get(i).getCount());
                            lineChartBean.setYear(ranklist.get(i).getYear());
                            arrayList.add(lineChartBean);
                        }
                        myLineChart = new MyLineChart(WillCollegeDetail.this, will_college_detail_chart, arrayList, lineChartType);
                    }
                }
                break;
            default:
                break;
        }

    }
}
